package com.richfun.boot.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	public final static String PATTERN_TIME = "yyyy-MM-dd HH:mm:ss";
	
	/**
	 * 转换时间为String
	 * */
	public static String format(Date date, String pattern) {
		date = date == null ? new Date() : date;
		pattern = CommonUtil.isBlank(pattern) ? PATTERN_TIME :pattern;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}
	/**
	 * 转换String时间为Date
	 * */
	public static Date parse(String date, String pattern) {
		if (CommonUtil.isBlank(date)) {
			return null;
		}
		pattern = CommonUtil.isBlank(pattern) ? PATTERN_TIME :pattern;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		try {
			return simpleDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
}
